package com.iphanta.community.mq;

import com.iphanta.community.common.message.CommunityMessage;
import com.iphanta.community.common.message.MessageContent;
import com.iphanta.community.common.utils.RedisKeyUtils;
import com.iphanta.community.mq.message.ChatMessage;
import com.iphanta.community.mq.message.DynamicFeed;
import com.iphanta.community.remote.service.TimService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 4/16/22 4:07 PM
 */
@Service
@RocketMQMessageListener(
        topic = "sys-chat-notifications",
        consumerGroup = "sys-chat-notifications-group",
        consumeThreadMax = 32
)
public class ChatMessageListener implements RocketMQListener<ChatMessage> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChatMessageListener.class);

    private TimService timService;

    private RedissonClient redissonClient;

    private RAtomicLong atomicLong;

    private static final Map<Integer, String> SYSTEM_CHAT_ACCOUNT = new HashMap<>();
    private static final Map<Integer, String> SYSTEM_CHAT_STATUS = new HashMap<>();
    static {
        SYSTEM_CHAT_ACCOUNT.put(0, "system-1001");
        SYSTEM_CHAT_ACCOUNT.put(1, "hand-pick-1002");

        SYSTEM_CHAT_STATUS.put(0, "system-chat-status");
        SYSTEM_CHAT_STATUS.put(1, "hand-pick-chat-status");
    }


    public ChatMessageListener(TimService timService, RedissonClient redissonClient) {
        this.timService = timService;
        this.redissonClient = redissonClient;
        this.atomicLong = this.redissonClient.getAtomicLong("Message_Seq");
    }

    @Override
    public void onMessage(ChatMessage message) {

        try {
            RMap<String, String> chatStatus = this.redissonClient.getMap(RedisKeyUtils.INSTANCE.key("chat_status", message.getTo()));
            String status = chatStatus.get(SYSTEM_CHAT_STATUS.get(message.getFrom()));
            Long msgSeq = this.atomicLong.get();

            if(!Objects.equals("1", status)) {
                CommunityMessage communityMessage = new CommunityMessage();
                communityMessage.setType(1);
                communityMessage.setMessageSeq(msgSeq);
                MessageContent content = new MessageContent();
                content.setText("Hi！我是精选小助手，你发布的动态被标记为“精选”后，我会及时通知你哦！");
                communityMessage.setContent(content);
                this.timService.sendMessage(SYSTEM_CHAT_ACCOUNT.get(message.getFrom()), message.getTo(), communityMessage);
                chatStatus.put(SYSTEM_CHAT_STATUS.get(message.getFrom()), "1");
            }
            LOGGER.info("chat message: {}", message);

            message.getCommunityMessage().setMessageSeq(msgSeq);

            this.timService.sendMessage(SYSTEM_CHAT_ACCOUNT.get(message.getFrom()), message.getTo(), message.getCommunityMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
        }

    }
}
